package cn.aaalei.utils;

import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.security.MessageDigest;
import java.util.*;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;


public class EncryptUtil {
    public static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
    public static final String KEY_ALGORITHM = "AES";

    public static String encrypt(String str) throws Exception {
        Key secretKeySpec = new SecretKeySpec(Base64.getDecoder().decode("PVm4KXehFo0E1TUZOeG1KA=="), KEY_ALGORITHM);
        Cipher instance = Cipher.getInstance(CIPHER_ALGORITHM);
        instance.init(1, secretKeySpec);
        return Base64.getEncoder().encodeToString(instance.doFinal(str.getBytes()));
    }

    public static String getSign(Map<String, String> hashMap){
        return EncryptUtil.sign(hashMap,"3b47d697bac24467a4fd7ab1");
    }
    public static String sign(Map<String, String> map, String str) {
        return sign(map, null, str);
    }

    public static String sign(Map<String, String> map, List<String> list, String str) {
        try {
            StringBuilder stringBuilder = new StringBuilder();
            List<String> arrayList = new ArrayList(map.size());
            arrayList.addAll(map.keySet());
            if (list != null && list.size() > 0) {
                for (String remove : list) {
                    arrayList.remove(remove);
                }
            }
            Collections.sort(arrayList);
            stringBuilder.append(str);
            for (String str2 : arrayList) {
                stringBuilder.append(str2);
                stringBuilder.append((String) map.get(str2));
            }
            stringBuilder.append(str);
            return byte2hex(getSHA1Digest(stringBuilder.toString()));
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    public static String utf8Encoding(String str, String str2) {
        try {
            return new String(str.getBytes(str2), "UTF8");
        } catch (Throwable e) {
            throw new IllegalArgumentException(e);
        }
    }

    private static byte[] getSHA1Digest(String str) throws IOException {
        try {
            return MessageDigest.getInstance("SHA-1").digest(str.getBytes("UTF8"));
        } catch (Throwable e) {
            throw new IOException(e);
        }
    }

    private static byte[] getMD5Digest(String str) throws IOException {
        try {
            return MessageDigest.getInstance("MD5").digest(str.getBytes("UTF8"));
        } catch (Throwable e) {
            throw new IOException(e);
        }
    }

    private static String byte2hex(byte[] bArr) {
        StringBuilder stringBuilder = new StringBuilder();
        for (byte b : bArr) {
            String toHexString = Integer.toHexString(b & 255);
            if (toHexString.length() == 1) {
                stringBuilder.append("0");
            }
            stringBuilder.append(toHexString.toUpperCase());
        }
        return stringBuilder.toString();
    }
}
